package com.example.auth;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.model.UserModel;
import com.example.auth.uitls.UserModelMap;
import com.example.auth.uitls.UserModelUtils;
import com.example.utils.PropertiesUtils;
import com.example.utils.Results;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname LoginUtils
 * @Description TODO
 * @Date 2022/5/29 14:27
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class LoginUtils {

        private static List<String> LoginFreeListUrls;


          /**
           * 初始化登录
           */
          public static void init(Router router){
                Login(router);
              FreeLoginUrlInit();
          }


          public static void Login(Router router){
              //登录
              router.route().handler(context->{
                  HttpServerRequest request=context.request();
                  String token=request.getHeader("Token");
                  UserModel userModel=null;
                  if(token != null){
                      userModel= UserModelMap.getUserModel(token);
                      if(userModel != null){
                          if(userModel.isIslocked()){
                              JSONObject obj= Results.error("当前用户:"+userModel.getUsername()+"已被锁定，请联系管理员");
                              context.json(obj);
                          }else {
                              UserModelUtils.addCurrentUser(userModel);
                          }
                      }
                  }
                  //开启登录
                  if(isEnabledLogin()){
                      String url=request.uri();
                      if(urlIsPass(url)){
                          //放行
                          context.next();
                      }else {
                          if(token == null){
                              JSONObject obj= Results.error("请先登录后在进行操作");
                              context.json(obj);
                          }
                          if(token != null){
                              if(userModel==null){
                                  JSONObject obj= Results.error("请先登录后在进行操作");
                                  context.json(obj);
                              }
                              if(userModel != null){
                                  UserModelUtils.addCurrentUser(userModel);
                                  //放行
                                  context.next();
                              }
                          }
                      }
                  }
              });
          }


        /**
         * 添加免登录地址
         * @param url
         */
        public static void addFreeLoginUrl(String url){
              FreeLoginUrlInit();
              LoginUtils.LoginFreeListUrls.add(url);
        }

        /**
         * 判断是否存在此url
         * @param url
         * @return
         */
        public static boolean existFreeLoginUrl(String url){
                if(LoginUtils.LoginFreeListUrls==null){
                    return false;
                }
                return LoginUtils.LoginFreeListUrls.contains(url);
        }

        /**
         * 删除url
         * @param url
         */
         public static void removeUrl(String url){
                if(LoginUtils.LoginFreeListUrls != null){
                    LoginUtils.LoginFreeListUrls.remove(url);
                }
         }


        /**
         * 登录地址初始化
         */
        public static void FreeLoginUrlInit(){
                if(LoginUtils.LoginFreeListUrls == null){
                    LoginUtils.LoginFreeListUrls=new ArrayList<>();
                }
        }


        /**
         * 获取是否开启登录
         * @return
         */
        private static boolean isEnabledLogin(){
            String login=PropertiesUtils.getProperty("login");
            if(login==null){
                return false;
            }
            return login.equals("true");
        }

        /**
         * 判断url是否能通过
         * @param url
         * @return
         */
        private static boolean urlIsPass(String url){
            if(LoginUtils.LoginFreeListUrls==null
                    || LoginUtils.LoginFreeListUrls.isEmpty()){
                return false;
            }
            if(url==null){
                return false;
            }
            if(url.contains("?")){
                url=url.substring(0,url.lastIndexOf("?"));
            }
            return LoginUtils.LoginFreeListUrls.contains(url);
        }

}
